<?php require_once('Connections/adminConn.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

// *** Redirect if username exists
$MM_flag="MM_insert";
if (isset($_POST[$MM_flag])) {
  $MM_dupKeyRedirect="accountExist.php";
  $loginUsername = $_POST['newUser'];
  $LoginRS__query = sprintf("SELECT username FROM `user` WHERE username=%s", GetSQLValueString($loginUsername, "text"));
  mysql_select_db($database_adminConn, $adminConn);
  $LoginRS=mysql_query($LoginRS__query, $adminConn) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);

  //if there is a row in the database, the username was found - can not add the requested username
  if($loginFoundUser){
    $MM_qsChar = "?";
    //append the username to the redirect page
    if (substr_count($MM_dupKeyRedirect,"?") >=1) $MM_qsChar = "&";
    $MM_dupKeyRedirect = $MM_dupKeyRedirect . $MM_qsChar ."requsername=".$loginUsername;
    header ("Location: $MM_dupKeyRedirect");
    exit;
  }
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "createAccountForm")) {
  $insertSQL = sprintf("INSERT INTO `user` (username, password) VALUES (%s, %s)",
                       GetSQLValueString($_POST['newUser'], "text"),
                       GetSQLValueString($_POST['newUserPass'], "text"));

  mysql_select_db($database_adminConn, $adminConn);
  $Result1 = mysql_query($insertSQL, $adminConn) or die(mysql_error());

  $insertGoTo = "accountCreated.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Design by Free CSS Templates
http://www.freecsstemplates.org
Released for free under a Creative Commons Attribution 2.5 License

Name       : FronzenAge
Description: A two-column, fixed-width template suitable for business sites and blogs.
Version    : 1.0
Released   : 20071108

-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>MWLReversi</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
<!--[if IE]>
<style type="text/css">
#sidebar #calendar {
	background-position: 0px 20px;
}
</style>
<![endif]-->
</head>
<body>
<div id="logo">
	<h1><a href="#">MWLReversi</a></h1>
	<h2></a>Gra Planszowa Online</h2>
</div>
<div id="menu">
	<ul>
		<li class="first"><a href="index.php?site=mainPage" accesskey="1" title="">Strona Główna</a></li>
		<li><a href="userPage.php" accesskey="2" title="">Użytkownik</a></li>
		<li><a href="index.php?site=adminPage" accesskey="3" title="">Administrator</a></li>
		<li><a href="index.php?site=playGamePage" accesskey="4" title="">Zagraj</a></li>
	</ul>
<?php 
$site;
if (is_null($_GET["site"]) == false)
$site= $_GET["site"];
else $site = "mainPage";
?>

	<div id="search">
		<!-- pole do wyszukiwania wyrzucic -->
	</div>
</div>
<hr />
<div id="banner"><img src="images/img04.jpg" alt="" width="960" height="147" /></div>
<!-- start page -->
<div id="page">
	<!-- start content -->
    
<form action="<?php echo $editFormAction; ?>" method="POST" name="createAccountForm">    
    <table width="350"  cellspacing="2" cellpadding="2">
  <caption>
    Utwórz nowe konto
  </caption>
  <tr>
    <th scope="row">login</th>
    <td><input name="newUser" type="text" size="20" maxlength="20" /></td>
  </tr>
  <tr>
    <th scope="row">hasło</th>
    <td>    <input name="newUserPass" type="password" size="20" maxlength="20" /></td>
  </tr>
  <tr>
    <th scope="row">    <input name="createUser" type="submit" value="utwórz konto" /></th>
    <td>&nbsp;</td>
  </tr>
</table>





    <input type="hidden" name="MM_insert" value="createAccountForm" />
    </form>
<!-- tutaj wstawie glowna stronę!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
	<!-- end content -->
	<!-- start sidebar -->
	<div id="sidebar">
    <?php
	include "userMenu.php";
	?>
<!-- boczny panel do wywalenia!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->		
	<!-- end sidebar -->
</div>
<!-- end page -->
<div id="footer">
	<p class="legal">Copyright (c) 2008 MWLReversi. All rights reserved.</p>
	<p class="credit">Designed by <a href="http://www.nodethirtythree.com/">NodeThirtyThree</a> + <a href="http://www.freecsstemplates.org/">Free CSS Templates</a></p>
</div>
</body>
</html>
